/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.Improvement;
import deatax.Player;
import deatax.Resource;
import deatax.Tile;
import deatax.network.Messages;
import deatax.unit.Unit;
import javax.media.opengl.GLAutoDrawable;

/**
 *
 * @author pson
 */
public abstract class GameState {

    protected String name;
    protected GameData data;

    public GameState(String name, GameData data) {
        this.name = name;
        this.data = data;
    }

    public void begin() {
    }

    public void pause() {
    }

    public void resume() {
    }

    public void die() {
    }

    public void finish() {
    }

    public void init(GLAutoDrawable glad) {
        if (data.getRender() != null) {
            data.getRender().init(glad);
        }
    }

    public void dispose(GLAutoDrawable glad) {
    }

    public void display(GLAutoDrawable glad) {
        if (data.getRender() != null) {
            if (data.getMiniboard() != null) {
                if (this instanceof RollState) {
                    data.getMiniboard().maximise();
                } else {
                    data.getMiniboard().minimise();
                }
            }
            data.getRender().display(glad);
        }
    }

    public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) {
        if (data.getRender() != null) {
            data.getRender().reshape(glad, i, i1, i2, i3);
        }
    }

    public String getName() {
        return name;
    }
    
    public void commonMessages(int type, String[] arg) {
        System.out.println("calling common: " + type + arg.length);
        switch (type) {
            case Messages.CLAIM: {
                String pName = arg[0];
                int tx = Integer.parseInt(arg[1]);
                int ty = Integer.parseInt(arg[2]);
                Player p = data.getRoster().getPlayer(pName);
                Tile t = data.getMap().getTile(tx, ty);
                t.setOwner(p);
            }
            break;
            case Messages.BUILD: {
                String iName = arg[0];
                int tx = Integer.parseInt(arg[1]);
                int ty = Integer.parseInt(arg[2]);
                Tile t = data.getMap().getTile(tx, ty);
                Improvement i = null;
                if (!iName.isEmpty()) {
                    i = data.getGame().getImprove(iName);
                }
                t.setImprovement(i);
                data.getGui().refreshContent();
            }
            break;
            case Messages.RESOURCE: {
                String pName = arg[0];
                Player p = data.getRoster().getPlayer(pName);
                for (int i = 1; i < arg.length; i += 2) {
                    Resource r = data.getGame().getResource(arg[i]);
                    int n = Integer.parseInt(arg[i+1]);
                    p.addResource(r, n);
                }
                data.getGui().refreshContent();
            }
            break;
            case Messages.UNIT: {
                String pName = arg[0];
                String uName = arg[1];
                int num = Integer.parseInt(arg[2]);
                int tx = Integer.parseInt(arg[3]);
                int ty = Integer.parseInt(arg[4]);
                
                Player p = data.getRoster().getPlayer(pName);
                Unit u = data.getGame().getUnit(uName);
                Tile t = data.getMap().getTile(tx, ty);
                
                if (num > 0) {
                    t.addUnit(p, u, num);
                    if (arg.length > 5) {
                        t.tireUnits(p, u, num);
                    }
                } else {
                    t.removeUnit(p, u, -num);
                }
                data.getGui().refreshContent();
            }
            break;
            case Messages.POPULATION: {
                int tx = Integer.parseInt(arg[0]);
                int ty = Integer.parseInt(arg[1]);
                int num = Integer.parseInt(arg[2]);
                
                Tile t = data.getMap().getTile(tx, ty);
                t.addPopulation(num);
                data.getGui().refreshContent();
            }
            break;
            case Messages.LOG: {
                String pName = arg[0];
                String message = arg[1];
                Player p = data.getRoster().getPlayer(pName);
                data.getGui().addMessage(p, message);
            }
            break;
            case Messages.END_TURN: {
                finish();
                data.getGui().refreshContent();
            }
            break;
            default:
                break;
        }
    }
}
